package com.bryant.bryant_weather.ui.home

import android.os.Handler
import android.os.Looper
import com.bryant.bryant_weather.base.BaseInteractor
import com.bryant.bryant_weather.bean.ChangeCityBean
import com.bryant.bryant_weather.utils.WeatherUtils
import com.qweather.sdk.Callback
import com.qweather.sdk.QWeather
import com.qweather.sdk.basic.Lang
import com.qweather.sdk.basic.Range
import com.qweather.sdk.basic.Unit
import com.qweather.sdk.parameter.air.AirV1Parameter
import com.qweather.sdk.parameter.geo.GeoCityTopParameter
import com.qweather.sdk.parameter.weather.WeatherParameter
import com.qweather.sdk.response.air.v1.AirV1CurrentResponse
import com.qweather.sdk.response.air.v1.AirV1HourlyResponse
import com.qweather.sdk.response.error.ErrorResponse
import com.qweather.sdk.response.geo.GeoCityTopResponse
import com.qweather.sdk.response.weather.WeatherHourlyResponse
import com.qweather.sdk.response.weather.WeatherNowResponse

class HomeInteractor : BaseInteractor<HomeView>() {

    // =============================== 获取首页天气数据 ========================================
    // 1. 定义内部接口提供回调方法
    interface GetWeatherData {
        // 1. 成功
        fun getWeatherDataSuccess(response: WeatherNowResponse)

        // 2. 失败
        fun getWeatherDataFailed(msg: String)
    }

    // 2. 发送请求
    fun getCityCurrentWeather(
        latitude: String,
        longitude: String,
        callback: GetWeatherData
    ) {
        // 1.准备参数
        val tempParam =
            WeatherUtils.formatLatLng(longitude) + ',' + WeatherUtils.formatLatLng(latitude)
        val parameter = WeatherParameter(tempParam).lang(Lang.ZH_HANT).unit(Unit.METRIC)

        // 2. 发送请求
        QWeather.instance.weatherNow(parameter, object : Callback<WeatherNowResponse> {
            override fun onSuccess(res: WeatherNowResponse) {
                // 天气请求成功， 通过Handler切换回主线程回调接口
                Handler(Looper.getMainLooper()).post {
                    callback.getWeatherDataSuccess(res)
                }
            }

            override fun onFailure(error: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getWeatherDataFailed(error.toString())
                }
            }

            override fun onException(e: Throwable) {
                Handler(Looper.getMainLooper()).post {
                    callback.getWeatherDataFailed(e.toString())
                }
                e.printStackTrace()
            }

        })
    }
    // ----------------------------------------------------------------------------------------------------------------------------------


    // =============================== 获取24小时天气数据 ========================================

    // 1.定义内部接口提供回调方法
    interface GetHourlyWeatherData {
        // 1. 获取24小时天气成功
        fun getHourlyWeatherDataSuccess(response: WeatherHourlyResponse)

        // 2. 获取24小时天气失败
        fun getHourlyWeatherDataFailed(msg: String)
    }

    // 2. 发送请求
    fun getCityHourlyWeather(
        latitude: String,
        longitude: String,
        callback: GetHourlyWeatherData
    ) {
        // 1. 准备参数
        val tempParam =
            WeatherUtils.formatLatLng(longitude) + ',' + WeatherUtils.formatLatLng(latitude)
        val parameter = WeatherParameter(tempParam).lang(Lang.ZH_HANS).unit(Unit.METRIC)
        // 2. 发送请求
        QWeather.instance.weather24h(parameter, object : Callback<WeatherHourlyResponse> {
            //  通过Handler切换回主线程回调接口
            override fun onSuccess(res: WeatherHourlyResponse) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataSuccess(res)

                }
            }

            override fun onFailure(e: ErrorResponse) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataFailed(e.toString())
                }
            }

            override fun onException(err: Throwable) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataFailed(err.toString())
                }
                err.printStackTrace()
            }

        })
    }
    //--------------------------------------------------------------------------------------------------------------------------------------


    // =============================== 获取空气质量数据 ========================================
    // 1. 定义内部接口提供回调方法
    interface GetAirQualityData {
        // 1. 获取空气质量成功
        fun getCurrentAirQualitySuccess(response: AirV1CurrentResponse)

        // 2. 获取空气质量失败
        fun getCurrentAirQualityFailed(msg: String)
    }

    // 2. 发送请求获取空气质量数据
    fun getCurrentAirQualityData(
        latitude: String,
        longitude: String,
        callback: GetAirQualityData
    ) {
        // 1. 准备参数
        val parameter = AirV1Parameter(
            WeatherUtils.formatLatLng(latitude).toDouble(),
            WeatherUtils.formatLatLng(longitude).toDouble()
        )

        // 2. 发送请求
        QWeather.instance.airCurrent(parameter, object : Callback<AirV1CurrentResponse> {
            override fun onSuccess(res: AirV1CurrentResponse?) {
                Handler(Looper.getMainLooper()).post {
                    res?.let {
                        callback.getCurrentAirQualitySuccess(res)
                    }
                }
            }

            override fun onFailure(e: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCurrentAirQualityFailed(e.toString())
                }
            }

            override fun onException(err: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCurrentAirQualityFailed(err.toString())
                }
            }
        })
    }
    // ----------------------------------------------------------------------------------------------------------------------------------



    // =============================== 获取24小时空气质量数据 ========================================
    // 1. 定义内部接口提供回调方法

    interface Get24HourAirQualityData {
        // 1. 获取24小时空气质量成功
        fun get24HourAirQualityDataSuccess(response: AirV1HourlyResponse)
        // 2. 获取24小时空气质量失败
        fun get24HourAirQualityDataFailed(msg: String)
    }

    // 2. 发送请求获取24小时空气质量数据
    fun get24HourAirQualityData(
        latitude: String,
        longitude: String,
        callback: Get24HourAirQualityData
    ){
        // 1. 准备参数
        val parameter = AirV1Parameter(
            WeatherUtils.formatLatLng(latitude).toDouble(),
            WeatherUtils.formatLatLng(longitude).toDouble()
        )
        QWeather.instance.airHourly(parameter, object : Callback<AirV1HourlyResponse> {
            override fun onSuccess(res: AirV1HourlyResponse?) {
               Handler(Looper.getMainLooper()).post {
                   res?.let {
                       callback.get24HourAirQualityDataSuccess(res)
                   }
               }
            }

            override fun onFailure(err: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.get24HourAirQualityDataFailed(err.toString())
                }
            }

            override fun onException(e: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.get24HourAirQualityDataFailed(e.toString())
                }
                e?.printStackTrace()
            }
        })
    }
    // -------------------------------------------------------------------------------------------



    // =============================== 获取城市列表 ========================================
    // 1. 定义内部接口提供回调方法
    interface GetCityListData {
        // 1. 获取城市列表成功
        fun getCityListSuccess(response:GeoCityTopResponse)
        // 2. 获取城市列表失败
        fun getCityListFailed(msg: String)
    }

    // 2. 获取城市列表数据
    fun getCityListData(callback: GetCityListData) {
        // 1. 准备参数
        var param = GeoCityTopParameter()
        param.number(20).range(Range.CN).lang(Lang.ZH_HANS)
        QWeather.instance.geoCityTop(param, object : Callback<GeoCityTopResponse> {
            override fun onSuccess(response: GeoCityTopResponse) {
                if (response.code == "200") {
                    Handler(Looper.getMainLooper()).post {
                        callback.getCityListSuccess(response)
                    }
                } else {
                    Handler(Looper.getMainLooper()).post {
                        callback.getCityListFailed(response.code)
                    }
                }
            }

            override fun onFailure(error: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCityListFailed(error.toString())
                }
            }

            override fun onException(e: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCityListFailed(e.toString())
                }
            }
        })
    }

    // ------------------------------------------------------------------------------------------------------------------------------------
}